<template>
  <template v-for="subItem in menuList" :key="subItem.path">
    <el-sub-menu
      v-if="subItem.children && subItem.children.length > 0"
      :index="subItem.path"
    >
      <template #title>
        <el-icon>
          <component :is="subItem.meta.icon"></component>
        </el-icon>
        <span>{{ $t(subItem.meta.title) }}</span>
      </template>
      <SubMenu :menuList="subItem.children" />
    </el-sub-menu>
    <el-menu-item
      v-else
      :index="subItem.path"
      @click="handleClickMenu(subItem)"
    >
      <el-icon>
        <component :is="subItem.meta.icon"></component>
      </el-icon>
      <template #title>
        <span>{{ $t(subItem.meta.title) }}</span>
      </template>
    </el-menu-item>
  </template>
</template>

<script setup lang="ts">
import type { MenuOptions } from '@/types/menu'
import { useRouter } from 'vue-router'
defineProps<{ menuList: MenuOptions[] }>()

const router = useRouter()
const handleClickMenu = (subItem: MenuOptions) => {
  if (subItem.meta.isLink) window.open(subItem.meta.isLink, '_blank')
  else router.push(subItem.path)
}
</script>

<style lang="scss">
/**在elementplus中使子组件高亮的时候父组件也高亮 */
.el-sub-menu.is-active > .el-sub-menu__title {
  color: var(--el-color-primary);
}

.el-sub-menu__title,
.el-menu-item {
  &:hover {
    color: var(--el-color-primary);
    background: var(--el-color-primary-light-9);
  }
}
</style>
